package com.example.library.service;
import com.example.library.enums.BookStatusEnum;
import com.example.library.mapper.BookInfoMapper;
import com.example.library.model.BookInfo;
import com.example.library.model.PageRequest;
import com.example.library.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-16
 * Time: 23:24
 */
@Slf4j
@Service//告诉spring存对象
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    @RequestMapping("/getBookListByPage")
    public PageResult<BookInfo> selectBookInfoByPage(PageRequest pageRequest) {
        if (pageRequest == null) {
            return null;
        }
        Integer size = pageRequest.getPageSize();
        List<BookInfo> bookInfos =  bookInfoMapper.selectBookInfoByPage(pageRequest.getOffset(), size);
        if (bookInfos!=null && bookInfos.size() > 0) {
            for (BookInfo b :
                    bookInfos) {
                b.setStatusCN(BookStatusEnum.getNameByCode(b.getStatus()).getName());
            }
        }
        return new PageResult<>(bookInfos, bookInfoMapper.count(), pageRequest);
    }
    public Integer addBook(BookInfo bookInfo) {
        Integer result = 0;
        try {
            result = bookInfoMapper.insertBook(bookInfo);
        } catch (Exception e) {
            log.error("添加图书错误e: {}", e.getMessage());
        }
        return result;
    }
    public BookInfo queryBookInfoById(Integer bookId) {
        BookInfo bookInfo = null;
        try {
            bookInfo = bookInfoMapper.selectBookById(bookId);
        } catch (Exception e) {
            log.error("查找图书错误e: {}", e.getMessage());
        }
        return bookInfo;
    }
    public Integer updateBook(BookInfo bookInfo) {
        Integer ret = 0;
        try {
            ret = bookInfoMapper.updateBook(bookInfo);
        } catch (Exception e) {
            log.error("更新图书失败, e:{}",  e.getMessage());
        }
        return ret;
    }
    public Integer batchDelete(List<Integer> ids) {
        Integer result = 0;
        try {
            result = bookInfoMapper.deleteBook(ids);
        } catch (Exception e) {
            log.error("e, 删除图书失败: {}", e.getMessage());
        }
        return result;

    }
}